package com.zcxy.sys.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxy.core.bean.PageQuery;
import com.zcxy.mybatis.service.BaseServiceImpl;
import com.zcxy.sys.entity.SysLog;
import com.zcxy.sys.mapper.SysLogMapper;
import com.zcxy.sys.pojo.SysLogVo;
import com.zcxy.sys.service.ISysLogService;
import com.zcxy.util.BeanCopierUtil;
import com.zcxy.util.FileUtil;
import com.zcxy.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统日志表Service业务层处理
 *
 * @author zcc
 * @email 860474958@qq.com
 * @date 2020-06-04
 */
@Service
@Slf4j
public class SysLogServiceImpl extends BaseServiceImpl<SysLogMapper, SysLog> implements ISysLogService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public IPage<SysLogVo> queryPageList(PageQuery<SysLog> queryVo) {
        QueryWrapper queryWrapper = new QueryWrapper(queryVo.getT() == null ? new SysLog() : queryVo.getT());
        queryWrapper.orderByAsc("create_time");
        Page page = new Page(queryVo.getPageNo(), queryVo.getPageSize());
        IPage<SysLog> sysUserVoIPage = baseMapper.selectPage(page, queryWrapper);
        return BeanCopierUtil.copyPage(sysUserVoIPage, SysLogVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SysLogVo> queryList(SysLog queryVo) {

        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSysLog(SysLog queryVo) {
        if (StringUtils.isEmpty(queryVo.getId())) {
            queryVo.setId(RandomUtil.randomString(16));
        }
      /*    //TODO 具体新增业务限制各自实现
         baseMapper.insert(sysLog);*/
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSysLog(SysLog queryVo) {
        if (StringUtils.isEmpty(queryVo.getId())) {
            queryVo.setId(RandomUtil.randomString(16));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeSysLog(String id) {
        baseMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeAllSysLog(List<String> ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        baseMapper.delete(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void download(SysLog queryVo, HttpServletResponse response) throws IOException {
        List<SysLog> sysLogs = baseMapper.selectList(new QueryWrapper<>(queryVo));
        List<Map<String, Object>> list = new ArrayList<>();
        for (SysLog sysLogExcel : sysLogs) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("", sysLogExcel.getId());
            map.put("描述", sysLogExcel.getDescription());
            map.put("异常详情", sysLogExcel.getExceptionDetail());
            map.put("日志类型", sysLogExcel.getLogType());
            map.put("方法", sysLogExcel.getMethod());
            map.put("参数", sysLogExcel.getParams());
            map.put("ip", sysLogExcel.getRequestIp());
            map.put("", sysLogExcel.getTime());
            map.put("用户名", sysLogExcel.getUsername());
            map.put("url", sysLogExcel.getAddress());
            map.put("浏览器", sysLogExcel.getBrowser());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

}
